package com.jason.jasonapp;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import com.jason.jasonapp.tools.JasonCurrentBostonDownloader;
import com.jason.jasonapp.tools.JasonPhoneStateListener;
import com.jason.jasonapp.tools.JasonRadioPlayer;

import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
		SurfaceHolder.Callback {

	boolean led_off = true;
	Button btn_flashlight = null;
	Button btn_kbos = null;
	static Camera camera = null;
	JasonPhoneStateListener listener = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		btn_flashlight = (Button) this.findViewById(R.id.button_flashlight);
		btn_flashlight.setOnClickListener(this);
		btn_kbos = (Button) this.findViewById(R.id.button_kbos);
		btn_kbos.setOnClickListener(this);
		Button btn_test = (Button) this.findViewById(R.id.button_test);
		btn_test.setOnClickListener(this);
		listener = new JasonPhoneStateListener(this);
		TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		tel.listen(listener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int sel = v.getId();
		switch (sel) {
		case R.id.button_flashlight:
			if (led_off) {
				try {
					camera = Camera.open(CameraInfo.CAMERA_FACING_BACK);
					Parameters p = camera.getParameters();
					p.setFlashMode(Parameters.FLASH_MODE_TORCH);
					camera.setParameters(p);
					SurfaceView sfce = (SurfaceView)this.findViewById(R.id.surfaceView1);
					SurfaceHolder sfceh = sfce.getHolder();
					// sfceh.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
					sfceh.addCallback(this);

					camera.setPreviewDisplay(sfceh);
					camera.startPreview();
					// CameraPreview prev = new CameraPreview(this, camera);

					led_off = false;
				} catch (Exception e) {
					Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
				}
			} else {
				camera.stopPreview();
				camera.release();
				led_off = true;
			}
			break;
		case R.id.button_kbos:
			KBOS();
			break;
		case R.id.button_test:
			testfunc();
			break;
		}
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,	int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub

	}

	public void printSignalStrength(SignalStrength strength) {
		TextView tv = (TextView) this.findViewById(R.id.textView_rssi);
		tv.setText("" + JasonPhoneStateListener.AsuToRssi(strength.getGsmSignalStrength()) + "dBm");
	}

	public void KBOS() {
		Intent jasonRadioPlayer = new Intent(this, JasonRadioPlayer.class);
		if (JasonRadioPlayer.serviceStarted) {
			stopService(jasonRadioPlayer);
		} else {
			//jasonRadioPlayer.putExtra("URL", JasonRadioPlayer.stations.get("KBOS_TWR").getURL());
			jasonRadioPlayer.putExtra("KEY", "KBOS_TWR");
			startService(jasonRadioPlayer);
		}
	}

	public void testfunc() {
		JasonCurrentBostonDownloader jcbd = new JasonCurrentBostonDownloader(this);
		jcbd.start();
	}
}
